/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import entities.Categorie;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author chocoseb
 */
public class CategoriesTableModel extends AbstractTableModel {

    private String[] _columnsNames = {
        "Categorie", "Parent"
    };
    private ArrayList<Categorie> _listCategories = Categorie.getCategorieList();

    @Override
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    @Override
    public String getColumnName(int p_col) {
        String result;
        if (p_col < this.getColumnCount()) {
            result = _columnsNames[p_col];
        } else {
            result = "";
        }
        return result;
    }

    public int getColumnCount() {
        return _columnsNames.length;
    }

    public int getRowCount() {
        return _listCategories.size();
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        Object result = null;
        switch (columnIndex) {
            case 0:
                result = _listCategories.get(rowIndex);
                break;
            case 1:
                if (_listCategories.get(rowIndex).getParent() == null){
                    result = "Aucun";
                }else{
                    result = _listCategories.get(rowIndex).getParent();
                }
                break;
        }
        return result;
    }

    public ArrayList<Categorie> getList(){
        return _listCategories;
    }

    public void add(Categorie c){
        _listCategories.add(c);
        fireTableDataChanged();
    }

    public void delete(int index){
        _listCategories.remove(index);
        fireTableDataChanged();
    }

    public Categorie get(int index){
        return _listCategories.get(index);
    }

    public void update(){
        fireTableDataChanged();
    }
}
